<template>
  <div class="subject">
    <span>{{ num1 }}</span>
    <span>+</span>
    <span>{{ num2 }}</span>
    <span>=</span>
    <input
      type="number"
      v-model="result"
      :disabled="isShow"
    />&nbsp;&nbsp;&nbsp;
    <button @click="subFn" :disabled="isShow">提交</button>
  </div>
</template>

<script>
export default {
  props: ["index"],
  data() {
    return {
      num1: Math.floor(Math.random() * 21 + 0),
      num2: Math.floor(Math.random() * 21 + 0),
      result: null,
      isShow: false,
    };
  },
  methods: {
    subFn() {
      // 点击提交后禁用输入框和提交按钮
      this.isShow = true;
      // 判断输入结果是否正确
      let resultStr = this.num1 + this.num2 == this.result ? "正确" : "错误";
      // 抛出事件 点击提交时触发父组件的事件并传入结果和此题的索引
      this.$emit("result", resultStr, this.index);
    },
  },
};
</script>

<style scoped>
.subject {
  margin: 5px;
  padding: 5px;
  font-size: 20px;
}
.subject span {
  display: inline-block;
  text-align: center;
  width: 20px;
}
.subject input {
  width: 50px;
  height: 20px;
}
</style>